package com.bwie.controller;

import com.bwie.exception.SystemException;
import com.bwie.pojo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("file")
public class FileController {

    public static String uploadUrl = "D:\\上海云计算\\专高二\\项目\\nebula_ykmn1_parent\\nebula_ykmn1_server\\src\\main\\webapp\\upload\\";

    /**
     * 上传图片方法
     * @param file
     * @return
     */
    @RequestMapping("uploadFile")
    @ResponseBody
    public Result uploadFile(MultipartFile file){
        String fileName = "";
        try {
            //原文件名获取
            String originalFilename = file.getOriginalFilename();
            //新文件名
            fileName = new Date().getTime() + originalFilename;
            File newFile = new File(uploadUrl+fileName);
            //赋值
            file.transferTo(newFile);
        } catch (IOException e) {
            throw new SystemException(e);
        }

        return Result.success("查询成功","/upload/" + fileName);
    }

}
